<template>
  <div id="song-menu">
    <div class="top-highquality">
      <!-- 模糊图片 -->
      <img
        class="bg-img img-percent"
        v-if="isList"
        v-lazy="playlists[0].coverImgUrl"
        :alt="playlists[0].tag"
      >
      <div class="left-img img-border">
        <!-- 左侧小图 -->
        <img
          class="img-percent"
          v-if="isList"
          v-lazy="playlists[0].coverImgUrl"
          :alt="playlists[0].tag"
        >
      </div>
      <div class="right-info">
        <button class="jinping">
          <a-icon type="crown" />
          精品歌单
        </button>
        <div style="margin-top: 10px;" class="text-hidden">
          {{ isList && playlists[0].name }}
        </div>
        <!-- <div class="text-hidden">
          {{ playlists[0].description }}
        </div> -->
      </div>
    </div>
    <!-- tag标签 -->
    <!-- <TagList
      ref="tag"
      :hotList="hotList"
      @tagName="tagName"
    /> -->
    <div class="tag-type">
      <tag-popover
        placement="right"
        :titleName="selected"
        :showPopover="showPopover"
        :loading="loading"
        @showDetail="getCatList"
        @closeDetail="closeDetail"
      >
        <template>
          <div
            class="tag-all"
            v-for="(item, key) in allCat"
            :key="key"
          >
            <div class="tag-title">
              <a-icon :type="typeIcon(key)" />
              <span>{{ key }}</span>
            </div>
            <ul>
              <li
                class="text-hidden"
                v-for="iten in item"
                :key="iten.name"
                @click="changeTag(iten.name)"
              >
                <button class="pointer" :class="{ 'isActive': iten.name === selected }">
                  {{ iten.name }}
                </button>
              </li>
            </ul>
          </div>
        </template>
      </tag-popover>
      <checkable-tag :tagList="hotList" @checked="name => changeTag(name)" :selectTag="selected" />
    </div>
    <!-- 歌单列表 -->
    <ContentCard
      ref="card"
      :dataList="playLists"
      :dataTotal="playTotal"
      @pageInfo="pageInfo"
      @goDetail="goDetail"
    />
  </div>
</template>

<script>
import { tophighquality, playList, hotPlayTag, catList } from '@/api';
import TagPopover from '@/components/Library/TagPopover';
import CheckableTag from '@/components/Library/CheckableTag';
import ContentCard from '@/components/Content/ContentCard';

export default {
  name: 'SongMenu',

  components: {
    ContentCard,
    CheckableTag,
    TagPopover
  },

  data() {
    return {
      playlists: [],
      // 歌单列表
      playLists: [],
      // 歌单总数
      playTotal: 0,
      // 热门标签
      hotList: [],
      // 处理后的全部标签列表
      allCat: {},
      // 控制弹框显示隐藏
      showPopover: false,
      // 按钮loading效果
      loading: false,
      // 选中标签
      selected: '华语'
    };
  },

  created() {
    this.$nextTick(() => {
      this.getHotTag();
      this.getTopHigh();
      this.getPlayList();
    });
  },

  computed: {
    isList() {
      return this.playlists && this.playlists.length;
    }
  },

  methods: {
    // 歌单标签列表
    async getHotTag() {
      const res = (await hotPlayTag()).data;
      this.hotList = Object.freeze(res.tags);
    },
    /**
     * limit: '取出歌单数量', 默认为1,
     * tag: '根据标签查询, 默认为华语'
     */
    async getTopHigh(tag, limit) {
      const res = (await tophighquality(tag, limit)).data;
      this.playlists = Object.freeze(res.playlists);
    },
    /**
     * order: hot或new,分别对应最新和最热 , 默认为 'hot',
     * cat: tag, 比如 " 华语 "、" 古风 " 、" 欧美 "、" 流行 ",
     * offset: 0为第一页,50为第二页以此类推
     */
    async getPlayList(cat, order, offset) {
      const res = (await playList(cat, order, offset)).data;
      if (res.code === 200) {
        this.playLists = Object.freeze(res.playlists);
        this.playTotal = res.total;
      }
    },
    // 歌单分类
    async getCatList() {
      // 气泡框显示后,再次点击按钮不在进行调用接口
      if (this.showPopover) {
        this.showPopover = false;
        return;
      }
      this.showPopover = true;
      // 如果已经调用过接口,避免多次请求相同接口
      if (this.allCat && Object.keys(this.allCat).length) return;
      this.loading = true;
      const res = (await catList()).data;
      console.log(res);
      Object.keys(res.categories).forEach(item => {
        const categorySub = res.sub.filter(iten => iten.category == item);
        this.allCat[res.categories[item]] = categorySub;
      });
      
      this.loading = false;
    },
    changeTag(name) {
      if (this.showPopover) {
        this.showPopover = false;
      }
      // 重置页码显示
      this.$refs.card.current = 1;
      this.selected = name;
      this.getTopHigh(name);
      this.getPlayList(name);
    },
    // 获取分页操作信息
    pageInfo(info) {
      const offset = (info.page - 1) * 50;
      this.getPlayList(this.selected, undefined, offset);
      // 事件总线
      this.$eventBus.$emit('goTop');
    },
    // 判断icon编码
    typeIcon(icon) {
      switch (icon) {
        case '语种':
          return 'global';
        case '风格':
          return 'skin';
        case '场景':
          return 'shop';
        case '情感':
          return 'smile';
        default:
          return 'appstore';
      }
    },
    // 关闭标签弹框
    closeDetail(value) {
      this.showPopover = value;
    },
    goDetail(id) {
      this.$router.push({
        name: 'playlistdetail',
        query: { id }
      })
    }
  },
};
</script>

<style lang="less" scoped>
#song-menu {
  .top-highquality {
    position: relative;
    display: flex;
    align-items: center;
    overflow: hidden;
    height: 200px;
    border-radius: 15px;
    .bg-img {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      z-index: 1;
      filter: blur(45px);
    }
    .left-img {
      height: 80%;
      margin: 0 20px;
      overflow: hidden;
      z-index: 2;
    }
    .right-info {
      z-index: 2;
      .jinping {
        display: block;
        height: 30px;
        padding: 0 16px;
        outline: 0;
        border: 1px solid #e7aa5a;
        color: #e7aa5a;
        background-color: transparent;
        border-radius: 15px;
      }
    }
  }
  .tag-type {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 10px 0;
    .tag-all {
      width: 740px;
      max-width: 740px;
      display: flex;
      justify-content: space-between;
      padding: 10px 0;
      border-bottom: 2px solid #e6e6e6;
      // align-items: center;
      font-size: 14px;
      &:last-child {
        border-bottom: none;
      }
      .tag-title {
        width: 20%;
        color: #cfcfcf;
        font-size: 24px;
        padding-left: 15px;
        span {
          font-size: 12px;
          margin-left: 5px;
          cursor: default;
        }
      }
      ul {
        width: 80%;
        display: flex;
        flex-wrap: wrap;
        // justify-content: space-between;
        align-items: center;
        margin-bottom: 0;
        li {
          width: 20%;
          font-size: 12px;
          &:hover {
            color: #ec4141;
          }
          button {
            border: none;
            padding: 3px 10px;
            background-color: transparent;
          }
        }
      }
    }
    .isActive {
      background-color: #fdf5f5 !important;
      color: #ec4141;
      border-radius: 13px;
    }
  }
}
</style>